package com.sy.server.controller;

import com.sy.server.dto.CostList;
import com.sy.server.dto.CostWithoutDetail;
import com.sy.server.model.Cost;
import com.sy.server.privilege.anno.Permission;
import com.sy.server.service.ICostService;
import com.sy.server.service.IExcelService;
import com.sy.server.vo.CostWithStr;
import com.sy.server.vo.Day;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping(value = {"/cost","/excel"})
@CrossOrigin
@Permission
public class CostController {
    @Autowired
    private ICostService service;

    @Autowired
    private IExcelService excelService;

    @Permission
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ResponseEntity<Void> add(@RequestBody Cost cost){
        service.add(cost);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Permission
    @RequestMapping(value = "/del/{day}",method = RequestMethod.DELETE)
    public ResponseEntity<Void> del(@PathVariable String day){
        service.del(day);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Permission
    @RequestMapping(value = "/getXDaysAvg",method = RequestMethod.POST)
    public ResponseEntity<Double> getXDaysAvg(@RequestBody Day day){
        double avg=service.getXDaysAvg(day);
        return new ResponseEntity<>(avg,HttpStatus.OK);
    }

    /**
     * 根据指定年月查询支出列表
     * @param year
     * @param month
     * @return
     */
    @Permission
    @RequestMapping(value = "/queryPayList/{year}/{month}",method = RequestMethod.GET)
    public ResponseEntity<List<Cost>> queryPayList(@PathVariable Integer year,@PathVariable Integer month){
        List<Cost> list=service.queryPayList(year,month);
        return new ResponseEntity<>(list,HttpStatus.OK);
    }

    @Permission
    @RequestMapping(value = "/queryPayListWithoutDetail",method = RequestMethod.POST)
    public ResponseEntity<CostList> queryPayListWithoutDetail(@RequestBody Day day){
        CostList lists=service.queryPayListWithoutDetail(day);
        return new ResponseEntity<>(lists,HttpStatus.OK);
    }

    @Permission
    @RequestMapping(value = "/updateByDate",method = RequestMethod.PUT)
    public ResponseEntity<Void> updateByDate(@RequestBody CostWithStr costWithStr){
        service.updateByDate(costWithStr);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @RequestMapping(value = "/verifyDate/{date}",method = RequestMethod.GET)
    public ResponseEntity<Boolean> verifyDate(@PathVariable String date){
        boolean flag=service.verifyDate(date);
        return new ResponseEntity<>(flag,HttpStatus.OK);
    }

    /*------------------show页----------------------*/
    /**
     * 根据指定年月日查询过去几天所有支出
     * @param day
     * @return
     */
    @Permission
    @RequestMapping(value = "/getTotalPayList",method = RequestMethod.POST)
    public ResponseEntity<List<Double>> getTotalPayList(@RequestBody Day day){
        List<Double> totals=service.getTotalPayList(day);
        return new ResponseEntity<>(totals,HttpStatus.OK);
    }

    /**
     * 得到图像横坐标列表（日期列表）
     * @param day
     * @return
     */
    @RequestMapping(value = "/getXDays",method = RequestMethod.POST)
    public ResponseEntity<List<String>> getXDays(@RequestBody Day day){
        List<String> list=service.getXDays(day);
        return new ResponseEntity<>(list,HttpStatus.OK);
    }

    @Permission
    @RequestMapping(value = "/sumEachOther/{year}/{month}",method = RequestMethod.GET)
    public ResponseEntity<CostWithoutDetail> sumEachOther(@PathVariable Integer year,@PathVariable Integer month){
        CostWithoutDetail cwd=service.sumEachOther(year,month);
        return new ResponseEntity<>(cwd,HttpStatus.OK);
    }

    @Permission
    @RequestMapping(value = "/writeExcel",method = RequestMethod.GET)
    public ResponseEntity<String> writeExcel(){
        String fileName=excelService.writeExcel(0,0);
        return new ResponseEntity<>(fileName,HttpStatus.OK);
    }

    /*-------------------------------------*/
    /**
     * 根据指定年月查询所有支出
     * @param year
     * @param month
     * @return
     */
    @Deprecated
    @RequestMapping(value = "/getTotalPay/{year}/{month}",method = RequestMethod.GET)
    public ResponseEntity<Double> getTotalPay(@PathVariable Integer year,@PathVariable Integer month){
        double total=service.getTotalPay(year,month);
        return new ResponseEntity<>(total,HttpStatus.OK);
    }
}
